package com.purengage.ipilih.lib;

import java.util.ArrayList;

import com.purengage.ipilih.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EntryAdapter extends ArrayAdapter<Item> {

	private ArrayList<Item> items;
	private LayoutInflater vi;

	public EntryAdapter(Context context,ArrayList<Item> items) {
		super(context,0, items);
		this.items = items;
		vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		final Item i = items.get(position);
		if (i != null) {
			if(i.isSection()){
				SectionItem si = (SectionItem)i;
				v = vi.inflate(R.layout.list_item_section, null);

				v.setOnClickListener(null);
				v.setOnLongClickListener(null);
				v.setLongClickable(false);
				
				final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
				sectionView.setText(si.getTitle());
				
			}else{
				EntryItem ei = (EntryItem)i;
				v = vi.inflate(R.layout.list_item_entry, null);
				
				final ImageView img = (ImageView) v.findViewById(R.id.imgSurvey);
				final TextView title = (TextView)v.findViewById(R.id.textSurvey);
				final TextView subtitle = (TextView)v.findViewById(R.id.surveyTutup);
				final TextView subtitle2 = (TextView)v.findViewById(R.id.persen);
				final TextView id = (TextView) v.findViewById(R.id.id);
				
				if(img != null) img.setImageResource(ei.draw);
				if(title != null) title.setText(ei.title);
				if(subtitle != null) subtitle.setText(ei.subtitle);
				if(subtitle2 != null) subtitle2.setText(ei.subtitle2);
				if(id != null) id.setText(ei.id);							
				
			}
		}
		return v;
	}

}
